package com.picture.wallpaper.core.commons;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private int code;
    private String message;
    private T data;

    public Result(T data) {
        this.code = 200;
        this.message = "success";
        this.data = data;
    }

    public Result(T data, boolean success, String message) {
        if (success) {
            this.code = 200;
            this.message = "success";
        } else {
            this.code = 500;
            this.message = message;
        }
        this.data = data;
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(500, message);
    }

    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message);
    }
}

//@GetMapping("/user/{id}")
//public Result<User> getUserById(@PathVariable int id) {
//    User user = userService.getUserById(id);
//    if (user == null) {
//        return Result.fail("User not found");
//    }
//    return new Result<>(user);
//}
//
//@PostMapping("/login")
//public Result<String> login(@RequestBody LoginRequest request) {
//    boolean isValid = userService.authenticate(request.getUsername(), request.getPassword());
//    if (isValid) {
//        // 返回登录成功的结果
//        return new Result<>("Login successful");
//    } else {
//        // 返回登录失败的结果，并自定义错误状态码和提示信息
//        return Result.fail(401, "Invalid username or password");
//    }
//}